package com.hellobike.haxing.modules.hello;

import com.alibaba.fastjson.JSON;
import com.hellobike.haxing.modules.hello.response.*;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.http.HttpEntity;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import java.math.BigDecimal;

@Component
@ConfigurationProperties(prefix = "hello")
@Data
@Slf4j
public class HelloDataExchange {
    private RestTemplate restTemplate = new RestTemplate();

    /**
     * 打款接口
     */
    private String settle;

    /**
     * 打款查询接口
     */
    private String settleQuery;


    /**
     * 绑卡查询接口
     */
    private String bindCardQuery;


    /**
     * 业务系统标识
     */
    private String systemCode = "K4";

    private String AppId;

    private String AppSecret;

    /**
     * 绑卡查询
     *
     * @param guid
     * @param mobile
     * @param realName
     * @param idNo
     * @param bankNo
     * @return
     */
    public BindCardQueryResponse bindCardQuery(String guid, String mobile, String realName, String idNo, String bankNo) {
        BindCardQueryParam param = new BindCardQueryParam();
        param.setGuid(guid);
        MultiValueMap<String, String> headers = new LinkedMultiValueMap<>();
        headers.set("AppId", AppId);
        headers.set("AppSecret", AppSecret);
        headers.set("Content-Type", MediaType.APPLICATION_JSON_VALUE);
        HttpEntity<String> httpEntity = new HttpEntity<>(JSON.toJSONString(param), headers);
        String responseStr = restTemplate.postForObject(bindCardQuery, httpEntity, String.class);
        BindCardQueryResponse helloResponse = null;
        if (!StringUtils.isEmpty(responseStr)) {
            helloResponse = JSON.parseObject(responseStr, BindCardQueryResponse.class);
            return helloResponse;
        } else {
            helloResponse = new BindCardQueryResponse();
            helloResponse.setCode(-1);
            helloResponse.setMsg("请求失败");
        }
        return helloResponse;
    }

    /**
     * @param guid         用户表里面的guid
     * @param bankNo       银行卡卡号
     * @param settleAmount 打款金额
     * @param businessNo   打款流水号，唯一约束
     * @return
     */
    public SettltResponse settle(String guid, String bankNo, BigDecimal settleAmount, String businessNo) {
        SettleParam param = new SettleParam();
        param.setBindingAccount(bankNo);
        param.setSettleAmount(settleAmount);
        param.setSettleOrderNo(businessNo);
        param.setGuid(guid);
        MultiValueMap<String, String> headers = new LinkedMultiValueMap<>();
        headers.set("AppId", AppId);
        headers.set("AppSecret", AppSecret);
        headers.set("Content-Type", MediaType.APPLICATION_JSON_VALUE);
        HttpEntity<String> httpEntity = new HttpEntity<>(JSON.toJSONString(param), headers);
        String responseStr = restTemplate.postForObject(settle, httpEntity, String.class);
        log.info("结算结果：{}", responseStr);
        SettltResponse helloResponse = null;
        if (!StringUtils.isEmpty(responseStr)) {
            helloResponse = JSON.parseObject(responseStr, SettltResponse.class);
            return helloResponse;
        } else {
            helloResponse = new SettltResponse();
            helloResponse.setCode(-1);
            helloResponse.setMsg("请求失败");
        }
        return helloResponse;
    }

    /**
     * @param guid       用户表里面的guid
     * @param businessNo 打款流水号，唯一约束
     * @return
     */
    public SettltQueryResponse settleQuery(String guid, String businessNo) {
        SettleQueryParam param = new SettleQueryParam();
        param.setSettleOrderNo(businessNo);
        param.setGuid(guid);
        MultiValueMap<String, String> headers = new LinkedMultiValueMap<>();
        headers.set("AppId", AppId);
        headers.set("AppSecret", AppSecret);
        headers.set("Content-Type", MediaType.APPLICATION_JSON_VALUE);
        HttpEntity<String> httpEntity = new HttpEntity<>(JSON.toJSONString(param), headers);
        String responseStr = restTemplate.postForObject(settleQuery, httpEntity, String.class);
        log.info("结算结果：{}", responseStr);
        SettltQueryResponse helloResponse = null;
        if (!StringUtils.isEmpty(responseStr)) {
            helloResponse = JSON.parseObject(responseStr, SettltQueryResponse.class);
            return helloResponse;
        } else {
            helloResponse = new SettltQueryResponse();
            helloResponse.setCode(-1);
            helloResponse.setMsg("请求失败");
        }
        return helloResponse;
    }
}
